/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.ThuePhong;


import DTO.*;
import Server.QLKS_Interface;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import Define.*;
import GUI.ShowReport;
import GUI.Utilities;

/**
 *
 * @author Mr
 */
public final class JIFThuePhong extends javax.swing.JInternalFrame {

    private JDesktopPane desktop;
    DefaultTableModel modelKhachDatPhong;
    DefaultTableModel modelPhongDeThue;
    DefaultTableModel modelPhonThue;
    String ServerURL;
    QLKS_Interface QI;

    /**
     * Creates new form JIFThuePhong
     */
    public JIFThuePhong() throws Exception {
        initComponents();
        this.setSize(880, 560);
        ServerURL = "qlks";
        try {
            QI = (QLKS_Interface) Naming.lookup(ServerURL);
        } catch (NotBoundException | MalformedURLException | RemoteException ex) {
            Logger.getLogger(JIFDatPhong.class.getName()).log(Level.SEVERE, null, ex);
        }
        showPhongDeThue();
        showTablePhongThue();
        huyPhieuDatQuaHan();
        modelKhachDatPhong = QI.LoadTableModel(Define.YC_LAYDS_KHACHDATPHONG);
        tbKhachDatPhong.setModel(modelKhachDatPhong);
    }

    public void showPhongDeThue() {
        try {
            modelPhongDeThue = QI.LoadTableModel(Define.YC_LAYDS_PHONGDETHUE);
        } catch (RemoteException ex) {
            Logger.getLogger(JIFThuePhong.class.getName()).log(Level.SEVERE, null, ex);
        }
        tbPhongTrong_thue.setModel(modelPhongDeThue);
    }

    public void showTablePhongThue() {
        Vector column = new Vector();
        column.add("Mã Phòng");
        column.add("Tên Loại Phòng");
        column.add("Đơn Giá");
        modelPhonThue = new DefaultTableModel(column, 0);
        tbPhongChon_thue.setModel(modelPhonThue);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnPhieuThue = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbKhachDatPhong = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        tfTenKhachHang_thue = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        tfCMND_thue = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        tfEmail_thue = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        tfSoDienThoai_thue = new javax.swing.JTextField();
        dpNgayThue_thue = new org.jdesktop.swingx.JXDatePicker();
        jLabel13 = new javax.swing.JLabel();
        btLapPhieu_thue = new javax.swing.JButton();
        btThoat_thue = new javax.swing.JButton();
        jLabel14 = new javax.swing.JLabel();
        cbbSoKhach_thue = new javax.swing.JComboBox();
        jLabel15 = new javax.swing.JLabel();
        dpNgayTra_thue = new org.jdesktop.swingx.JXDatePicker();
        btLapPhieuMoi_thue = new javax.swing.JButton();
        jLabel16 = new javax.swing.JLabel();
        tfTriGia_thue = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        tbPhongChon_thue = new javax.swing.JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbPhongTrong_thue = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("LẬP PHIẾU THUÊ PHÒNG");

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Khách đã đặt phòng"));

        tbKhachDatPhong.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tbKhachDatPhong.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbKhachDatPhongMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tbKhachDatPhong);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin khách"));

        jLabel9.setText("Tên Khách Hàng");

        jLabel10.setText("CMND");

        tfCMND_thue.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tfCMND_thueKeyTyped(evt);
            }
        });

        jLabel11.setText("Email");

        jLabel12.setText("Số điện thoại");

        dpNgayThue_thue.setDate(new Date(System.currentTimeMillis()));
        dpNgayThue_thue.setEditable(false);

        jLabel13.setText("Ngày Thuê");

        btLapPhieu_thue.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/1371825243_Apply.png"))); // NOI18N
        btLapPhieu_thue.setText("Lập Phiếu");
        btLapPhieu_thue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLapPhieu_thueActionPerformed(evt);
            }
        });

        btThoat_thue.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/close.png"))); // NOI18N
        btThoat_thue.setText("Thoát");
        btThoat_thue.setPreferredSize(new java.awt.Dimension(79, 23));
        btThoat_thue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThoat_thueActionPerformed(evt);
            }
        });

        jLabel14.setText("Số khách");

        cbbSoKhach_thue.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30" }));

        jLabel15.setText("Ngày Trả");

        dpNgayTra_thue.setDate(new Date(System.currentTimeMillis()));
        dpNgayTra_thue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dpNgayTra_thueActionPerformed(evt);
            }
        });

        btLapPhieuMoi_thue.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Sync.png"))); // NOI18N
        btLapPhieuMoi_thue.setText("Lập mới");
        btLapPhieuMoi_thue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLapPhieuMoi_thueActionPerformed(evt);
            }
        });

        jLabel16.setText("Trị Giá");

        tfTriGia_thue.setEditable(false);
        tfTriGia_thue.setBackground(new java.awt.Color(204, 204, 204));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel13)
                            .addComponent(jLabel9)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel14)
                                .addComponent(jLabel15)
                                .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(34, 34, 34)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(dpNgayThue_thue, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(tfTenKhachHang_thue, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cbbSoKhach_thue, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(dpNgayTra_thue, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(tfTriGia_thue, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addGap(34, 34, 34)
                                .addComponent(tfEmail_thue, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addGap(34, 34, 34)
                                .addComponent(tfCMND_thue, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel12)
                                .addGap(34, 34, 34)
                                .addComponent(tfSoDienThoai_thue, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(22, 22, 22))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(btLapPhieuMoi_thue)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btLapPhieu_thue)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btThoat_thue, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(8, 8, 8)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(tfTenKhachHang_thue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(tfCMND_thue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfEmail_thue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel12)
                    .addComponent(tfSoDienThoai_thue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(dpNgayThue_thue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dpNgayTra_thue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel14)
                    .addComponent(cbbSoKhach_thue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(tfTriGia_thue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btLapPhieu_thue)
                    .addComponent(btLapPhieuMoi_thue)
                    .addComponent(btThoat_thue, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jScrollPane3.setBorder(javax.swing.BorderFactory.createTitledBorder("Phòng thuê"));

        tbPhongChon_thue.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.red));
        tbPhongChon_thue.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbPhongChon_thue.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbPhongChon_thueMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tbPhongChon_thue);

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin phòng để thuê"));

        tbPhongTrong_thue.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tbPhongTrong_thue.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbPhongTrong_thueMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbPhongTrong_thue);

        javax.swing.GroupLayout pnPhieuThueLayout = new javax.swing.GroupLayout(pnPhieuThue);
        pnPhieuThue.setLayout(pnPhieuThueLayout);
        pnPhieuThueLayout.setHorizontalGroup(
            pnPhieuThueLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnPhieuThueLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnPhieuThueLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnPhieuThueLayout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 496, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(pnPhieuThueLayout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 425, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        pnPhieuThueLayout.setVerticalGroup(
            pnPhieuThueLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnPhieuThueLayout.createSequentialGroup()
                .addGroup(pnPhieuThueLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnPhieuThueLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnPhieuThue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnPhieuThue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tbKhachDatPhongMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbKhachDatPhongMouseClicked
        // TODO add your handling code here:
        int row = tbKhachDatPhong.getSelectedRow();
        tfTenKhachHang_thue.setText(tbKhachDatPhong.getValueAt(row, 1).toString());
        tfCMND_thue.setText(tbKhachDatPhong.getValueAt(row, 2).toString());
        tfEmail_thue.setText(tbKhachDatPhong.getValueAt(row, 3).toString());
        tfSoDienThoai_thue.setText(tbKhachDatPhong.getValueAt(row, 4).toString());
    }//GEN-LAST:event_tbKhachDatPhongMouseClicked

    private void tfCMND_thueKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfCMND_thueKeyTyped
        // TODO add your handling code here:
        try {
            //modelKhachDatPhong = KhachHang_BUS.laykhachDatPhong(tfCMND_thue.getText());
            modelKhachDatPhong = QI.LoadTableModel(Define.YC_LAYDS_KHACHDATPHONG, tfCMND_thue.getText());
        } catch (RemoteException ex) {
            Logger.getLogger(JIFThuePhong.class.getName()).log(Level.SEVERE, null, ex);
        }
        tbKhachDatPhong.setModel(modelKhachDatPhong);
    }//GEN-LAST:event_tfCMND_thueKeyTyped

    private void btLapPhieu_thueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLapPhieu_thueActionPerformed
        // TODO add your handling code here:
        //Kiểm tra ô nhập cmnd có được nhập không
        String CMND = tfCMND_thue.getText();
        if (CMND.equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Bạn phải nhập CMND!");
            return;
        }
        if (tfTenKhachHang_thue.getText().equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Bạn phải nhập tên của khách!");
            return;
        }
        //kiểm tra khách hàng có phải đã đặt phòng không
        int rows = tbKhachDatPhong.getRowCount();

        int row = tbKhachDatPhong.getSelectedRow();
        if (rows > 0) {
            String maphieuthueMax;
            String maphieuThue;
            try {
                //maphieuthueMax = PhieuThuePhong_BUS.laymaMax();
                maphieuthueMax = QI.LayMaMax(Define.YC_LAYMAMAX_PHIEUTHUEPHONG);
                if (maphieuthueMax.equals("")) {
                    maphieuThue = Utilities.taoMaTuDong("MPT000000", 3);
                } else {
                    maphieuThue = Utilities.taoMaTuDong(maphieuthueMax, 3);
                }
                String maKhach = tbKhachDatPhong.getValueAt(row, 0).toString();
                SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
                Date dngayden = dpNgayThue_thue.getDate();
                Date dngaytra = dpNgayTra_thue.getDate();
                String ngaythue = format.format(dngayden);
                String ngaytra = format.format(dngaytra);
                String soKhach = cbbSoKhach_thue.getSelectedItem().toString();

                if (dngaytra.before(dngayden)) {
                    JOptionPane.showMessageDialog(rootPane, "Ngày đi phải lớn hơn hoặc bằng ngày đến!");
                    return;
                }
                //Them CT PhieuThue

                String mactphieumax;
                String mactPhieu;

                int row_chon = tbPhongChon_thue.getRowCount();
                if (row_chon <= 0) {
                    JOptionPane.showMessageDialog(rootPane, "Bạn phải chọn phòng cho khách!");
                    return;
                }

                Float dongia;
                long songaythue = (dngaytra.getTime() - dngayden.getTime()) / 86400000 + 1;
                Float trigia;
                long tongtrigia = 0;
                for (int i = 0; i < row_chon; i++) {
                    dongia = Float.parseFloat(tbPhongChon_thue.getValueAt(i, 2).toString());
                    trigia = dongia * songaythue;
                    String maP = (String) tbPhongChon_thue.getValueAt(i, 0);
                    String strTriGia = String.format("%.0f", trigia);
                    themBangDoanhThu(maP, strTriGia);
                    tongtrigia += trigia;
                }
                //long tongtrigia = (long) dongia * songaythue;
                tfTriGia_thue.setText(Long.toString(tongtrigia));
                PhieuThuePhong_DTO p = new PhieuThuePhong_DTO();
                p.setMaphieuThue(maphieuThue);
                p.setMaKhach(maKhach);
                p.setNgayThue(ngaythue);
                p.setNgayTra(ngaytra);
                p.setSoKhach(soKhach);
                p.setTriGia(tfTriGia_thue.getText());
                //KhachHang_BUS.capNhatTinhTrangKhach(maKhach, "Đang thuê");
                QI.CapNhatTinhTrangKhach(maKhach, "Đang thuê");
                //PhieuThuePhong_BUS.Insert(p);
                QI.InsertPhieuThuePhong(p);
                for (int i = 0; i < row_chon; i++) {
                    //mactphieumax = CTPhieuThuePhong_BUS.laymaMax();
                    mactphieumax = QI.LayMaMax(Define.YC_LAYMAMAX_CTPHIEUTHUEPHONG);
                    if (mactphieumax.equals("")) {
                        mactPhieu = Utilities.taoMaTuDong("MCTPT000000", 5);
                    } else {
                        mactPhieu = Utilities.taoMaTuDong(mactphieumax, 5);
                    }
                    String maPhong = tbPhongChon_thue.getValueAt(i, 0).toString();
                    CTPhieuThuePhong_DTO ct = new CTPhieuThuePhong_DTO();
                    ct.setMactphieuThue(mactPhieu);
                    ct.setMaphieuThue(maphieuThue);
                    ct.setMaPhong(maPhong);
                    //CTPhieuThuePhong_BUS.Insert(ct);
                    QI.InsertCTPhieuThuePhong(ct);
                    //Phong_BUS.capnhattinhtrangDat(maPhong, "Đang thuê");
                    QI.CapNhatTinhTrangPhong(maPhong, "Đang thuê");
                }
                //String maphieudat = PhieuDatPhong_BUS.layMaPhieuDat(maKhach);
                String maphieudat = QI.LayMaPhieu(Define.YC_LAYMA_PHIEUDATPHONG, maKhach);
                //CTPhieuDatPhong_BUS.Delete(maphieudat);
                QI.DeleteCTPhieuDatPhong(maphieudat);
                //PhieuDatPhong_BUS.Delete(maphieudat);
                QI.DeletePhieuDatPhong(maphieudat);
                tbKhachDatPhong.setModel(modelKhachDatPhong);
                JOptionPane.showMessageDialog(rootPane, "Lập phiếu thành công!");
                //modelKhachDatPhong = KhachHang_BUS.layDSKhachDatPhong();
                modelKhachDatPhong = QI.LoadTableModel(Define.YC_LAYDS_KHACHDATPHONG);
                tbKhachDatPhong.setModel(modelKhachDatPhong);
                new ShowReport().Show_ReportPhieuThue(maKhach);
                //QI.ShowPhieuThuePhong(maKhach);
            } catch (Exception ex) {
                Logger.getLogger(JIFThuePhong.class.getName()).log(Level.SEVERE, null, ex);
            }

            //nếu khách hàng chưa đặt phòng mà còn phòng thì cho thuê mới
        } else {
            String makhach;
            String makhachmax;
            try {
                //makhachmax = KhachHang_BUS.laymaMax();
                makhachmax = QI.LayMaMax(Define.YC_LAYMAMAX_KHACHHANG);
                if (makhachmax.equals("")) {
                    makhach = Utilities.taoMaTuDong("MK000000", 2);
                } else {
                    makhach = Utilities.taoMaTuDong(makhachmax, 2);
                }
                String tenKhach = tfTenKhachHang_thue.getText();
                String sdt = tfSoDienThoai_thue.getText();
                String email = tfEmail_thue.getText();
                String tinhtrang = "Đang thuê";

                String maphieuthueMax;
                String maphieuThue;

                //maphieuthueMax = PhieuThuePhong_BUS.laymaMax();
                maphieuthueMax = QI.LayMaMax(Define.YC_LAYMAMAX_PHIEUTHUEPHONG);
                if (maphieuthueMax.equals("")) {
                    maphieuThue = Utilities.taoMaTuDong("MPT000000", 3);
                } else {
                    maphieuThue = Utilities.taoMaTuDong(maphieuthueMax, 3);
                }

                SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
                Date dngayden = dpNgayThue_thue.getDate();
                String ngaythue = format.format(dngayden);
                Date dngaytra = dpNgayTra_thue.getDate();
                String ngaytra = format.format(dngaytra);
                String soKhach = cbbSoKhach_thue.getSelectedItem().toString();
                int row_chon = tbPhongChon_thue.getRowCount();
                if (row_chon <= 0) {
                    JOptionPane.showMessageDialog(rootPane, "Bạn phải chọn phòng cho khách!");
                    return;
                }
                float dongia;
                long songaythue = (dngaytra.getTime() - dngayden.getTime()) / 86400000 + 1;
                Float trigia;
                long tongtrigia = 0;
                for (int i = 0; i < row_chon; i++) {
                    //dongia += Float.parseFloat(tbPhongChon_thue.getValueAt(i, 2).toString());
                    dongia = Float.parseFloat(tbPhongChon_thue.getValueAt(i, 2).toString());
                    trigia = dongia * songaythue;
                    String maP = (String) tbPhongChon_thue.getValueAt(i, 0);
                    String strTG = String.format("%.0f",trigia);
                    themBangDoanhThu(maP, strTG);
                    tongtrigia += trigia;
                }
                //long trigia = (long) dongia * songaythue;
                tfTriGia_thue.setText(Long.toString(tongtrigia));

                KhachHang_DTO kh = new KhachHang_DTO();
                kh.setMaKhachHang(makhach);
                kh.setTenKhachHang(tenKhach);
                kh.setCmnd(CMND);
                kh.setDienThoai(sdt);
                kh.setEmail(email);
                kh.setTinhTrang(tinhtrang);
                //KhachHang_BUS.Insert(kh);
                QI.InsertKhachHang(kh);

                PhieuThuePhong_DTO p = new PhieuThuePhong_DTO();
                p.setMaphieuThue(maphieuThue);
                p.setMaKhach(makhach);
                p.setNgayThue(ngaythue);
                p.setNgayTra(ngaytra);
                p.setSoKhach(soKhach);
                p.setTriGia(tfTriGia_thue.getText());
                //PhieuThuePhong_BUS.Insert(p);
                QI.InsertPhieuThuePhong(p);

                String mactphieumax;
                String mactPhieu;
                for (int i = 0; i < row_chon; i++) {
                    //mactphieumax = CTPhieuThuePhong_BUS.laymaMax();
                    mactphieumax = QI.LayMaMax(Define.YC_LAYMAMAX_CTPHIEUTHUEPHONG);
                    if (mactphieumax.equals("")) {
                        mactPhieu = Utilities.taoMaTuDong("MCTPT000000", 5);
                    } else {
                        mactPhieu = Utilities.taoMaTuDong(mactphieumax, 5);
                    }
                    String maPhong = tbPhongChon_thue.getValueAt(i, 0).toString();
                    CTPhieuThuePhong_DTO ct = new CTPhieuThuePhong_DTO();
                    ct.setMactphieuThue(mactPhieu);
                    ct.setMaphieuThue(maphieuThue);
                    ct.setMaPhong(maPhong);
                    //CTPhieuThuePhong_BUS.Insert(ct);
                    QI.InsertCTPhieuThuePhong(ct);
                    //Phong_BUS.capnhattinhtrangDat(maPhong, "Đang thuê");
                    QI.CapNhatTinhTrangPhong(maPhong, "Đang thuê");

                }
                JOptionPane.showMessageDialog(rootPane, "Lập phiếu thành công!");
                new ShowReport().Show_ReportPhieuThue(makhach);
                //QI.ShowPhieuThuePhong(makhach);
                btLapPhieuMoi_thueActionPerformed(evt);

            } catch (Exception ex) {
                Logger.getLogger(JIFThuePhong.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }//GEN-LAST:event_btLapPhieu_thueActionPerformed

    private void btThoat_thueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThoat_thueActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btThoat_thueActionPerformed

    private void btLapPhieuMoi_thueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLapPhieuMoi_thueActionPerformed
        // TODO add your handling code here:
        modelPhonThue.setNumRows(0);
        modelKhachDatPhong.setNumRows(0);
        tfTenKhachHang_thue.setText("");
        tfCMND_thue.setText("");
        tfSoDienThoai_thue.setText("");
        tfEmail_thue.setText("");
        tfTriGia_thue.setText("");
    }//GEN-LAST:event_btLapPhieuMoi_thueActionPerformed

    private void tbPhongTrong_thueMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbPhongTrong_thueMouseClicked
        // TODO add your handling code here:
        int row = tbPhongTrong_thue.getSelectedRow();
        Vector rowData = new Vector();
        String maPhong = (String) this.modelPhongDeThue.getValueAt(row, 0).toString();
        String tenLoaiPhong = (String) this.modelPhongDeThue.getValueAt(row, 1).toString();
        String donGia = (String) this.modelPhongDeThue.getValueAt(row, 2).toString();
        rowData.add(maPhong);
        rowData.add(tenLoaiPhong);
        rowData.add(donGia);
        modelPhonThue.addRow(rowData);
        tbPhongChon_thue.setModel(modelPhonThue);
        modelPhongDeThue.removeRow(row);
        showThanhTien();
    }//GEN-LAST:event_tbPhongTrong_thueMouseClicked

    private void tbPhongChon_thueMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbPhongChon_thueMouseClicked
        // TODO add your handling code here:
        int row = tbPhongChon_thue.getSelectedRow();
        Vector rowData = new Vector();
        String maPhong = (String) this.modelPhonThue.getValueAt(row, 0).toString();
        String tenLoaiPhong = (String) this.modelPhonThue.getValueAt(row, 1).toString();
        String donGia = (String) this.modelPhonThue.getValueAt(row, 2).toString();
        rowData.add(maPhong);
        rowData.add(tenLoaiPhong);
        rowData.add(donGia);
        modelPhongDeThue.addRow(rowData);
        tbPhongTrong_thue.setModel(modelPhongDeThue);
        modelPhonThue.removeRow(row);
        showThanhTien();
    }//GEN-LAST:event_tbPhongChon_thueMouseClicked

    private void dpNgayTra_thueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dpNgayTra_thueActionPerformed
        // TODO add your handling code here:
        Date dngayden = dpNgayThue_thue.getDate();
        Date dngaytra = dpNgayTra_thue.getDate();
        if (dngaytra.before(dngayden)) {
            JOptionPane.showMessageDialog(rootPane, "Ngày đi phải lớn hơn hoặc bằng ngày đến!");
            dpNgayTra_thue.setDate(dpNgayThue_thue.getDate());
            return;
        }
        showThanhTien();
    }//GEN-LAST:event_dpNgayTra_thueActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btLapPhieuMoi_thue;
    private javax.swing.JButton btLapPhieu_thue;
    private javax.swing.JButton btThoat_thue;
    private javax.swing.JComboBox cbbSoKhach_thue;
    private org.jdesktop.swingx.JXDatePicker dpNgayThue_thue;
    private org.jdesktop.swingx.JXDatePicker dpNgayTra_thue;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JPanel pnPhieuThue;
    private javax.swing.JTable tbKhachDatPhong;
    private javax.swing.JTable tbPhongChon_thue;
    private javax.swing.JTable tbPhongTrong_thue;
    private javax.swing.JTextField tfCMND_thue;
    private javax.swing.JTextField tfEmail_thue;
    private javax.swing.JTextField tfSoDienThoai_thue;
    private javax.swing.JTextField tfTenKhachHang_thue;
    private javax.swing.JTextField tfTriGia_thue;
    // End of variables declaration//GEN-END:variables

    private void themBangDoanhThu(String maP, String doanhThu) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
        Date ngayHienTai = new Date(System.currentTimeMillis());
        String ngayHT = format.format(ngayHienTai);
        String thangHienTai = ngayHT.substring(3);
        //String[] xyz = ngayHT.split("-");
        //String thangHienTai = xyz[1] + "-" + xyz[2];
        String maDT;
        //maDT = DoanhThuTheoLoaiPhong_BUS.layMaDT(maP, thangHienTai);
        maDT = QI.LayMaDoanhThu(maP, thangHienTai);
        System.out.println(maDT);
        if (maDT.equals("")) {
            DoanhThuTheoLoaiPhong_DTO dt = new DoanhThuTheoLoaiPhong_DTO();
            dt.setMaDT(maDoanhThu());
            dt.setThangNam(thangHienTai);
            //dt.setMaLoaiPhong(Phong_BUS.layMaLPTheoMP(maP));
            dt.setMaLoaiPhong(QI.LayMaLoaiPhongTheoMaPhong(maP));
            dt.setDoanhThu(doanhThu);
            //DoanhThuTheoLoaiPhong_BUS.Insert(dt);
            QI.InsertDTTheoLoaiPhong(dt);
        } else {
            //Float doanhThuCu = DoanhThuTheoLoaiPhong_BUS.LayDoanhThuTheoThangVaLoaiPhong(thangHienTai, Phong_BUS.layMaLPTheoMP(maP));
            Float doanhThuCu = QI.LayDoanhThuTheoThangVaLoaiPhong(thangHienTai, QI.LayMaLoaiPhongTheoMaPhong(maP));
            Float doanhThuMoi = Float.parseFloat(doanhThu) + doanhThuCu;
            String strDoanhThuMoi = String.format("%.0f",doanhThuMoi);
            //dt.setDoanhThu(doanhThuMoi.toString());
            //DoanhThuTheoLoaiPhong_BUS.UpdateDoanhThu(doanhThuMoi.toString(), maDT);
            QI.UpdateDoanhThu(strDoanhThuMoi, maDT);
        }
    }

    private String maDoanhThu() throws Exception {
        //String lastID = DoanhThuTheoLoaiPhong_BUS.layMaCuoiCung();
        String lastID = QI.LayMaMax(Define.YC_LAYMAMAX_DOANHTHU);
        String nextID = null;
        try {
            if (lastID != null) {
                nextID = Utilities.taoMaTuDong(lastID, 3);
            } else {
                nextID = Utilities.taoMaTuDong("MDT0000", 3);
            }
        } catch (Exception e) {
        }
        return nextID;
    }

    /**
     * @return the desktop
     */
    public JDesktopPane getDesktop() {
        return desktop;
    }

    /**
     * @param desktop the desktop to set
     */
    public void setDesktop(JDesktopPane desktop) {
        this.desktop = desktop;
    }

    private void showThanhTien() {
        Date dngayden = dpNgayThue_thue.getDate();
        Date dngaytra = dpNgayTra_thue.getDate();
        int row = tbPhongChon_thue.getRowCount();
        float dongia = 0;
        for (int i = 0; i < row; i++) {
            dongia += Float.parseFloat(tbPhongChon_thue.getValueAt(i, 2).toString());
        }
        long songaythue = (dngaytra.getTime() - dngayden.getTime()) / 86400000 + 1;
        long trigia = (long) dongia * songaythue;
        tfTriGia_thue.setText(Long.toString(trigia));
    }
    public void huyPhieuDatQuaHan() {
        SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
        Date dtoday = new Date(System.currentTimeMillis() - 100742784);
        String today = format.format(dtoday);
        try {
            //ArrayList<PhieuDatPhong_DTO> ds = PhieuDatPhong_BUS.layDSMaPhieuDatQuaHan(today);
            ArrayList<PhieuDatPhong_DTO> ds;
            ds = QI.layDSMaPhieuDatQuaHan(today);
            for (int i = 0; i < ds.size(); i++) {
                String maPhieuDat = ds.get(i).getMaPhieuDat().toString();
                ArrayList<Phong_DTO> dsmaPhong;
                //dsmaPhong = PhieuDatPhong_BUS.layDSMaPhongDat(maPhieuDat);
                dsmaPhong = QI.LayDSPhongDaDat(maPhieuDat);
                for (int j = 0; j < dsmaPhong.size(); j++) {
                    //Phong_BUS.capnhattinhtrangDat(dsmaPhong.get(j).getMaPhong().toString(), "Trống");
                    QI.CapNhatTinhTrangPhong(dsmaPhong.get(j).getMaPhong().toString(), "Trống");
                }
                //CTPhieuDatPhong_BUS.Delete(maPhieuDat);
                QI.DeleteCTPhieuDatPhong(maPhieuDat);
                //PhieuDatPhong_BUS.Delete(maPhieuDat);
                QI.DeletePhieuDatPhong(maPhieuDat);
            }

        } catch (Exception ex) {
            Logger.getLogger(JIFThuePhong.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
